﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;

using Ylh.Model;
using Ylh.BLL;
using System.Data;

public partial class Module_CustManger_CustInfo : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button9.OnClientClick = Window1.GetShowReference("CustFlow.aspx?CustID=" + Request.QueryString["CustID"].ToString(), "跟进情况");
        Button4.OnClientClick = Window2.GetShowReference("CustMeet.aspx?CustID=" + Request.QueryString["CustID"].ToString(), "约见情况");

        if (!IsPostBack)
        {
            ShowInfo(Convert.ToInt32(Request.QueryString["CustID"].ToString()));
            LoadFlowData(Request.QueryString["CustID"].ToString());
            LoadMetData(Request.QueryString["CustID"].ToString());
        }


        if (GetCustType() == "ProtectCust")
        {
            Button10.Hidden = true;
            Button11.Hidden = false;
            ServerStaffName.Hidden = ServerStaffTel.Hidden = true;
        }
        else if (GetCustType() == "IntentCust")
        {
            Button10.Hidden = false;
            Button11.Hidden = true;
            ServerStaffName.Hidden = ServerStaffTel.Hidden = true;
        }
        else if (GetCustType() == "SigningCust")
        {
            Button3.Hidden = Button10.Hidden = Button11.Hidden = true;
            Panel2.Enabled = Panel9.Enabled = Panel6.Enabled = false;
            LoadServer(Request.QueryString["CustID"].ToString());
        }
    }

    /// <summary>
    /// 加载客服人员
    /// </summary>
    /// <param name="p"></param>
    private void LoadServer(string CustID)
    {
        Ylh.BLL.CustBLL bllc = new Ylh.BLL.CustBLL(DbConnectionStr);
        Ylh.Model.TbCust model = bllc.GetModel(Convert.ToInt32(CustID));

        Ylh.BLL.StaffBLL blls = new StaffBLL(DbConnectionStr);

        if (model.ServerStaffID != null)
        {
            DataTable dt = blls.GetStaff(Convert.ToInt32(model.ServerStaffID));

            ServerStaffName.Text = dt.Rows[0]["staffName"].ToString();

            ServerStaffTel.Text = dt.Rows[0]["TelPhone"].ToString();

            ServerStaffName.Hidden = ServerStaffTel.Hidden = false;
        }



    }

    private string GetCustType()
    {
        switch (Request.QueryString["CustType"].ToString())
        {
            case "ProtectCust":
                return "ProtectCust";
            case "IntentCust":
                return "IntentCust";
            case "SigningCust":
                return "SigningCust";
            default:
                return "";
        }
    }


    private void LoadFlowData(string CustID)
    {
        CustFollowBLL bllf = new CustFollowBLL(DbConnectionStr);
        Grid3.DataSource = bllf.GetFlowData(CustID, StaffID.ToString());
        Grid3.DataBind();
    }



    private void LoadMetData(string CustID)
    {
        CustMettingBLL bllm = new CustMettingBLL(DbConnectionStr);

        Grid2.DataSource = bllm.GetMettingInfo(StaffID.ToString(), CustID);
        Grid2.DataBind();
    }



    public Ylh.Model.TbCust GetCust()
    {


        string CustName = this.CustName.Text;
        string CompanyName = this.CompanyName.Text;
        string TelPhone = this.TelPhone.Text;
        string Industry = this.Industry.Text;
        string CustAddress = this.CustAddress.Text;


        string No = this.No.Text;
        string OrdersCode = this.OrdersCode.Text;
        string ID = this.ID.Text;
        string Pwd = this.Pwd.Text;
        string QQ = this.QQ.Text;
        string mail = this.mail.Text;
        string Website = this.Website.Text;

        string Legal = this.Legal.Text;

        Ylh.Model.TbCust model = new Ylh.Model.TbCust();

        model.CustID = Convert.ToInt32(Request.QueryString["CustID"].ToString());
        model.CustName = CustName;
        model.CompanyName = CompanyName;
        model.TelPhone = TelPhone;
        model.Industry = Industry;
        model.CustAddress = CustAddress;

        if (!string.IsNullOrEmpty(this.PayCount.Text))
        {
            model.PayCount = decimal.Parse(this.PayCount.Text);
        }
        else
        {
            model.PayCount = 0;
        }


        model.No = No;
        model.OrdersCode = OrdersCode;
        model.ID = ID;
        model.Pwd = Pwd;
        model.QQ = QQ;
        model.mail = mail;
        model.Website = Website;


        if (!string.IsNullOrEmpty(this.ClearTime.Text))
        {
            model.ClearTime = DateTime.Parse(this.ClearTime.Text);
        }
        else
        {
            model.ClearTime = null;
        }


        model.Legal = Legal;



        return model;
    }

    private void ShowInfo(int CustID)
    {
        Ylh.BLL.CustBLL bllc = new Ylh.BLL.CustBLL(DbConnectionStr);
        Ylh.Model.TbCust model = bllc.GetModel(CustID);
        this.CustName.Text = model.CustName;
        this.CompanyName.Text = model.CompanyName;
        this.TelPhone.Text = model.TelPhone;
        this.Industry.Text = model.Industry;
        this.CustAddress.Text = model.CustAddress;
        this.PayCount.Text = model.PayCount.ToString();
        this.No.Text = model.No;
        this.OrdersCode.Text = model.OrdersCode;
        this.ID.Text = model.ID;
        this.Pwd.Text = model.Pwd;
        this.QQ.Text = model.QQ;
        this.mail.Text = model.mail;
        this.Website.Text = model.Website;

        if (model.IsProtect)
        {
            Label2.Text = "<font style='color:red'>是</font>";
        }
        else
        {
            Label2.Text = "<font style='color:red'>否</font>";
        }
        Label2.EncodeText = false;


        if (model.ClearTime != null)
        {
            this.ClearTime.SelectedDate = Convert.ToDateTime(model.ClearTime);
        }

        this.Legal.Text = model.Legal;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        CustBLL bllc = new CustBLL(DbConnectionStr);
        Ylh.Model.TbCust model = GetCust();
        if (bllc.UpdateCustInfoBuss(model))
        {
            Alert.Show("保存成功");
            return;
        };

        Alert.Show("保存失败,请联系管理员");
        return;
    }
    protected void Grid3_RowCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateFlow")
        {
            string str = Grid3.DataKeys[e.RowIndex][0].ToString();
            PageContext.RegisterStartupScript(Window1.GetShowReference("CustFlow.aspx?enable=false&CustID=" + Request.QueryString["CustID"].ToString() + "&CustFollow=" + str, "跟进情况"));
        }
    }

    protected void Grid2_RowCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateMetting")
        {
            string str = Grid2.DataKeys[e.RowIndex][0].ToString();
            PageContext.RegisterStartupScript(Window1.GetShowReference("CustMeet.aspx?enable=false&CustID=" + Request.QueryString["CustID"].ToString() + "&CustMettingID=" + str, "跟进情况"));
        }
    }

    protected void Button7_Click(object sender, EventArgs e)
    {
        LoadFlowData(Request.QueryString["CustID"].ToString());
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        List<string> li = base.GetCheckID(Grid3);


        if (li.Count == 0)
        {
            Alert.Show("当前没有选中任何记录");
        }

        CustFollowBLL bll = new CustFollowBLL(DbConnectionStr);
        bll.Delete(li);
        LoadFlowData(Request.QueryString["CustID"].ToString());

        Alert.Show("删除成功");

    }



    protected void Button6_Click(object sender, EventArgs e)
    {
        Ylh.BLL.CustMettingBLL bllm = new CustMettingBLL(DbConnectionStr);

        List<string> li = base.GetCheckID(Grid3);


        if (li.Count == 0)
        {
            Alert.Show("当前没有选中任何记录");
        }

        bllm.Delete(li);

        LoadMetData(Request.QueryString["CustID"].ToString());
    }


    //转移到客服经理
    protected void Button3_Click(object sender, EventArgs e)
    {
        Ylh.BLL.CustBLL bllc = new CustBLL(DbConnectionStr);
        bllc.UpdateTransferInfo(Request.QueryString["CustID"].ToString());
        Alert.Show("转交成功");
    }



    private string GetCondition()
    {
        string ConditionStr = string.Empty;

        if (!string.IsNullOrEmpty(CustName.Text) && string.IsNullOrEmpty(CustAddress.Text))
        {
            ConditionStr += "  CustName ='" + CustName.Text + "' ";
        }
        else if (!string.IsNullOrEmpty(CustName.Text) && !string.IsNullOrEmpty(CustAddress.Text))
        {
            ConditionStr += " ( CustName ='" + CustName.Text + "'  and CustAddress ='" + CustAddress.Text + "' ) ";
        }


        if (!string.IsNullOrEmpty(CompanyName.Text))
        {
            ConditionStr += " or CompanyName ='" + CompanyName.Text + "' ";
        }


        if (!string.IsNullOrEmpty(TelPhone.Text))
        {
            ConditionStr += " or TelPhone ='" + TelPhone.Text + "' ";
        }


        if (!string.IsNullOrEmpty(CustAddress.Text))
        {
            ConditionStr += " or CustAddress ='" + CustAddress.Text + "' ";
        }


        if (!string.IsNullOrEmpty(Legal.Text))
        {
            ConditionStr += " or Legal ='" + Legal.Text + "' ";
        }
        return ConditionStr;

    }




    /// <summary>
    /// 设定为保护客户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button10_Click(object sender, EventArgs e)
    {

        if (string.IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }
        if (string.IsNullOrEmpty(TelPhone.Text))
        {
            Alert.Show("客户联系电话不能为空");
            return;
        }
        if (string.IsNullOrEmpty(Industry.Text))
        {
            Alert.Show("客户行业不能为空");
            return;
        }
        if (string.IsNullOrEmpty(Legal.Text))
        {
            Alert.Show("客户法人代表不能为空");
            return;
        }

        if (string.IsNullOrEmpty(CustAddress.Text))
        {
            Alert.Show("客户地址不能为空");
            return;
        }
        //有可能是个体户,没有公司的,所以这个条件不是必须的
        //if (string.IsNullOrEmpty(CompanyName.Text))
        //{
        //    Alert.Show("客户公司名称不能为空");
        //    return;
        //}


        Ylh.BLL.CustBLL bllc = new CustBLL(DbConnectionStr);
        DataTable dt = bllc.GetCustByCodition(GetCondition());

        if (dt.Rows.Count > 0)
        {
            Alert.Show("当前客户已经被其他人保护");
            return;
        }


        if (!bllc.CheckProtect(RoleID.ToString(), StaffID.ToString()))
        {
            Alert.Show("您的保护客户数量已经超过最大权限");
        }

        //判断是否存在约见情况
        //Ylh.BLL.CustMettingBLL bll = new CustMettingBLL(DbConnectionStr);
        //if (!bll.GetCountmetting(StaffID.ToString(), Request.QueryString["CustID"].ToString()))
        //{
        //    Alert.Show("请先填写约见情况");
        //    return;
        //}



        //第二种判断是否存在约见情况
        Ylh.BLL.CustFollowBLL bllf = new CustFollowBLL(DbConnectionStr);

        if (!bllf.GetModelByMetting(Request.QueryString["CustID"].ToString()))
        {
            Alert.Show("请添加[上门拜访]或者[客户来访]的跟进情况");
            return;
        }



        //判断经理是否给予意见

        string str = bllc.GetOpinion(Request.QueryString["CustID"].ToString());

        if (string.IsNullOrEmpty(str))
        {
            Alert.Show("经理还没有给予意见,如果需要保护,请通知经理");
            return;
        }




        //保存客户信息
        Ylh.Model.TbCust model = GetCust();

        //获取客户的保护时间




        if (!bllc.UpdateCustInfoBuss(model))
        {
            Alert.Show("保存失败,请联系管理员");
            return;
        };

        if (bllc.UpdateProtectInfo(Request.QueryString["CustID"].ToString(), true))
        {
            Alert.Show("设置成功");
        }
        else
        {
            Alert.Show("设置不成功,请联系管理员");

        }
    }

    /// <summary>
    /// 设定为保护客户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button11_Click(object sender, EventArgs e)
    {
        Ylh.BLL.CustBLL bllc = new CustBLL(DbConnectionStr);
        
        //保存客户信息
        Ylh.Model.TbCust model = GetCust();
        if (!bllc.UpdateCustInfoBuss(model))
        {
            Alert.Show("保存失败,请联系管理员");
            return;
        };

       if( bllc.UpdateProtectInfo(Request.QueryString["CustID"].ToString(), false))
       {
           Alert.Show("设置成功");
       }
        
    }

    //刷新跟踪情况
    protected void Window1_Close(object sender, WindowCloseEventArgs e)
    {
        LoadFlowData(Request.QueryString["CustID"].ToString());
    }
    protected void Window2_Close(object sender, WindowCloseEventArgs e)
    {
        LoadMetData(Request.QueryString["CustID"].ToString());
    }

    protected void Button12_Click(object sender, EventArgs e)
    {
        LoadMetData(Request.QueryString["CustID"].ToString());
    }



}